#!/usr/bin/env python
# encoding: utf-8
"""
@author:shucheng
@file:string_util.py
@time:2024/1/4 22:20
@desc 字符串处理工具类
"""

import re


def convert_to_chinese(string):
    """
    将/u开头的字符转换为中文
    :param string:
    :return:
    """
    return string.encode('utf-8').decode('unicode_escape')


def lower_first(string):
    """
    首字母小写
    :param string:
    :return:
    """
    return string[0].lower() + string[1:]


def upper_first(string):
    """
    首字母大写
    :param string:
    :return:
    """
    return string[0].upper() + string[1:]


def lower_camel_case(string):
    """
    转小驼峰
    使用正则表达式将下划线后面的字母变成大写
    :param string:
    :return:
    """
    string = re.sub(r'_\w', lambda x: x.group()[1].upper(), string)
    return string


def upper_camel_case(string):
    """
    转大驼峰
    使用
    :param string:
    :return:
    """
    words = string.split('_')

    result = ''
    for word in words:
        if len(word) > 0:
            result += word[0].upper() + word[1:]

    return result


def remove_blank_line(string):
    """
    去除空行
    :param string:
    :return:
    """
    string = re.sub(r'(?m)^\s*\r?\n', '', string)
    return string


def trim_white_space_to_one_line(original_sql):
    """
    将原始sql去除空白字符后，放到一行里

    e.g.
    origial_sql为
    SELECT
        u.id AS userId,
        u.user_name AS userName,
        r.id AS roleId,
        r.role_name AS roleName
    FROM
        sys_user u
        INNER JOIN sys_user_role ur ON u.id = ur.user_id
        INNER JOIN sys_role r ON ur.role_id = r.id;

    结果变成：SELECT u.id AS userId, u.user_name AS userName, r.id AS roleId, r.role_name AS roleName FROM sys_user u INNER JOIN sys_user_role ur ON u.id = ur.user_id INNER JOIN sys_role r ON ur.role_id = r.id;

    :param original_sql:
    :return:
    """
    result_sql = re.sub(r'(\r\n|\n)\s*', " ", original_sql)
    return result_sql
